package threads;

import java.util.Random;
import java.util.concurrent.*;

/**
 * @author apple
 * @Classname CountdownLatchThreadTest
 * @Description 使用CountdownLatch创建线程
 * @Date 2020/4/23 11:21
 */
public class CountdownLatchThreadTest {

    public static void main(String[] args) {
        int threadCount =9;
        CountdownLatchThreadTest test = new CountdownLatchThreadTest();
//        test.countdownLatchThread(threadCount);

        test.countDownLatchTest2();
    }

    /**用法一**/
    public void countdownLatchThread(int threadCount) {
        /**使用CountdownLatch创建线程**/
        CountDownLatch countDownLatch = new CountDownLatch(threadCount);
        for (int i = 1; i <= threadCount; i++) {
            String threadName = "线程 " + i + " ";
            CountDownLatchThread countDownLatchThread = new CountDownLatchThread(countDownLatch, threadName);
            Thread thread = new Thread(countDownLatchThread);
            thread.start();
        }
        try {
            countDownLatch.await();
        } catch (InterruptedException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        System.out.println("--主线程的下一步--");
    }


    /**用法二**/
    public void countDownLatchTest2(){
        int threadCount = 1;
        CountDownLatch countDownLatch = new CountDownLatch(threadCount);

        for(int i=0;i<threadCount;i++){
            CountDownLatchThread thread = new CountDownLatchThread(countDownLatch,"子线程"+i);
            thread.run();
        }
        try{
            countDownLatch.await();
        }catch (InterruptedException e){
            System.out.println(e.getMessage());
        }
        System.out.println("子线程执行完成,同时开始多个线程");
        executeMultiThread(4);
}

/**使用线程池**/
    public void countDownLatchTest3(){
        int threadCount = 2;
        int corePoolSize = 1;
        int maximumPoolSize = 8;
        long keepAliveTime = 10;
        TimeUnit timeUnit = TimeUnit.SECONDS;
        BlockingQueue<Runnable> workQueue = new LinkedBlockingQueue<Runnable>(4);
        RejectedExecutionHandler handler = new ThreadPoolExecutor.AbortPolicy();
        ExecutorService pool = new ThreadPoolExecutor(corePoolSize, maximumPoolSize, keepAliveTime, timeUnit, workQueue, handler);
        CountDownLatch countDownLatch3 = new CountDownLatch(threadCount);

        for(int i=0;i<threadCount;i++){
            CountDownLatchThread newThread = new CountDownLatchThread(countDownLatch3,"子线程"+i);
            newThread.run();
            pool.execute(newThread);
        }
        try{
            countDownLatch3.await();
        }catch (InterruptedException e){
            System.out.println(e.getMessage());
        }finally {
            /**关闭线程池**/
            pool.shutdown();
        }
        System.out.println("子线程执行完成,同时开始多个线程");
        executeMultiThread(3);
    }

    private void executeMultiThread(int threadCount){
        for(int i=0;i<threadCount;i++){
            ThreadTask threadTask = new ThreadTask();
            threadTask.setCount((10-i)*100);
            threadTask.run();
        }
    }
}

/**
 * 线程类
 **/
class CountDownLatchThread implements Runnable {
    private CountDownLatch countDownLatch;
    private String name;

    public CountDownLatchThread(CountDownLatch countDownLatch,
                                String name) {

        /** TODO Auto-generated constructor stub**/
        this.countDownLatch = countDownLatch;
        this.name = name;
    }

    @Override
    public void run() {
        /** TODO Auto-generated method stub**/
        Random random = new Random();
        int ran = random.nextInt(10) * 100 + 200;
        System.out.println(name + " run start." + " sleep " + ran);

        try {
            Thread.sleep(ran);
        } catch (InterruptedException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        System.out.println(name + " stop.");

        countDownLatch.countDown();
    }


}
